﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BALayer;

namespace PGBT
{
    public partial class DanhMucKhachHang : Form
    {
        private KhachHang db = null;
        private DataSet ds = null;
        private BindingSource bs = null;

        private bool flag = false;

        public DanhMucKhachHang()
        {
            InitializeComponent();
            db = new KhachHang();
        }

        private void DanhMucKhachHang_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void LoadData()
        {
            ds = db.DanhSachKhachHang();
            bs = new BindingSource();
            bs.DataSource = ds.Tables[0];
            dgvKhachHang.DataSource = bs;
            dgvKhachHang.AutoResizeColumns();
        }

        private void dgvKhachHang_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int index = dgvKhachHang.CurrentCell.RowIndex;

            txtMaKH.Text = dgvKhachHang.Rows[index].Cells[0].Value.ToString();
            txtTenKH.Text = dgvKhachHang.Rows[index].Cells[1].Value.ToString();
            txtDiaChi.Text = dgvKhachHang.Rows[index].Cells[2].Value.ToString();
            txtSDT.Text = dgvKhachHang.Rows[index].Cells[3].Value.ToString();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            flag = true;

            txtTenKH.ReadOnly = false;
            txtDiaChi.ReadOnly = false;
            txtSDT.ReadOnly = false;

            btnAdd.Enabled = false;
            btnEdit.Enabled = false;
            btnDel.Enabled = false;

            txtMaKH.Clear();
            txtDiaChi.Clear();
            txtSDT.Clear();
            txtTenKH.Clear();

            btnSave.Enabled = true;
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            flag = false;

            txtMaKH.Enabled = false;
            txtTenKH.ReadOnly = false;
            txtDiaChi.ReadOnly = false;
            txtSDT.ReadOnly = false;

            btnEdit.Enabled = false;
            btnAdd.Enabled = false;
            btnDel.Enabled = false;
            btnSave.Enabled = true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            btnSave.Enabled = false;

            string error = "";

            if(flag)
            {
                if (db.ThemKhachHang(ref error, txtTenKH.Text.ToString(), txtDiaChi.Text.ToString(), txtSDT.Text.ToString()) == -1)
                    MessageBox.Show(error, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                if(!db.CapNhaKhachHang(ref error, Convert.ToInt32(txtMaKH.Text.ToString()),
                    txtTenKH.Text.ToString(), txtDiaChi.Text.ToString(), txtSDT.Text.ToString()))
                    MessageBox.Show(error, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            btnSave.Enabled = false;
            btnAdd.Enabled = true;
            btnEdit.Enabled = true;
            btnDel.Enabled = true;

            txtMaKH.ReadOnly = true;
            txtTenKH.ReadOnly = true;
            txtDiaChi.ReadOnly = true;
            txtSDT.ReadOnly = true;

            txtMaKH.Clear();
            txtDiaChi.Clear();
            txtSDT.Clear();
            txtTenKH.Clear();

            LoadData();
        }
    }
}
